module Q26_Test

using HPAT
using HDF5
using Base.Test
using CompilerTools
using ParallelAccelerator

#ParallelAccelerator.ParallelIR.set_debug_level(3)

function create_dataset_large(dataset_name)
    
    customer = [343955,343955,343955,483820,483820,483820,483820,483820,483820,483820,483820,483820,698613,698613,698613,698613,698613,698613,698613,698613,698613,698613,673934,193332,193332,193332,193332,193332,193332,193332,193332,95583,95583,95583,95583,95583,95583,95583,95583,304571,304571,304571,304571,416879,416879,416879,416879,416879,416879,416879,416879,416879,416879,416879,416879,310692,310692,310692,310692,310692,310692,310692,310692,310692,310692,310692,310692,310692,314350,314350,314350,314350,314350,314350,314350,662505,662505,662505,248991,248991,248991,248991,248991,248991,248991,248991,248991,248991,248991,248991,248991,248991,141896,141896,141896,141896,141896,141896,141896,141896,141896,141896,141896,46361,46361,46361,46361,46361,46361,46361,38570,38570,38570,38570,38570,38570,38570,38570,38570,38570,38570,38570,38570,99024,99024,99024,99024,99024,99024,99024,99024,99024,653630,653630,653630,281747,281747,281747,762560,762560,762560,762560,762560,762560,368702,368702,368702,368702,368702,368702,368702,368702,368702,368702,368702,368702,368702,368702,566857,566857,566857,566857,566857,566857,566857,455927,455927,455927,455927,455927,455927,455927,455927,455927,455927,455927,455927,455927,455927,62945,351048,351048,351048,351048,351048,588434,588434,588434,394875,394875,394875,394875,284166,284166,284166,284166,284166,284166,284166,284166]

    sale = [1,3,419,133,240,3080,377,5731,842,2660,317,664,272,3586,91,600,7936,37,3682,735,273,698,590,952,275,95,242,833,448,613,376,2,306,173,632,613,600,4917,4314,342,8794,003,631,45,758,251,1035,083,729,133,671,3882,86,133,4839,140,122,713,4760,6020,543,606,138,979,209,637,554,31,527,8179,121,978,714,081,1189,0394,6991,36,593,129,3128,296,91,4392,734,1899,73,623,8717,25,25,400,1,503,8461,569,395,540,647,713,1365,654,816,721,680,296,826,700,3605,7463,6466,624,5110,0989,6047,0567,7920,402,752,237,003,2647,1971,8157,030,3710,2,7,6496,398,107,259,367,403,2,246,5757,525,1689,7596,737,7901,701,2533,2340,516,2,0778,420,579,6046,9117,913,427,293,8811,152,138,91,3500,791,730,364,393,330,503,25,213,9918,343,54,7516,511,451,3133,20,241,458,4655,579,8513,3376,613,246,8697,133,191,190,8960,3932,8566,762,102,453,5521,6804,54,40,993,3542]

    item = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199]
    class = [1,4,3,3,2,1,3,4,3,2,2,2,3,3,2,1,15,11,16,3,15,2,3,4,5,2,1,6,2,1,5,4,2,7,2,3,2,2,7,1,2,1,13,1,1,6,7,3,2,1,2,2,1,1,2,2,12,1,16,3,11,2,3,11,12,11,15,3,2,1,4,2,4,1,5,1,3,2,3,3,11,16,3,5,4,3,11,15,3,15,4,10,5,3,3,2,13,4,8,8,3,5,6,3,8,2,2,1,3,3,2,2,4,2,11,1,2,10,2,13,2,3,5,11,4,5,3,13,3,11,2,3,8,2,3,2,3,3,3,4,3,1,4,7,16,5,12,5,3,13,3,10,1,4,2,15,3,2,2,3,2,6,2,6,5,2,4,7,11,1,4,3,16,11,5,3,1,15,1,4,3,1,16,5,8,2,8,3,2,7,11,15,8,9,11,1,3,3,4,3]
    category = [954798,74408,74408,74408,74408,941848,90882,90882,74408,74408,74408,74408,74408,90882,941848,941848,90882,90882,90882,74408,90882,74408,90882,74408,74408,941848,941848,954798,74408,74408,74408,74408,74408,91429743828,74408,74408,941848,941848,90882,74408,74408,74408,90882,74408,74408,91429743828,91429743828,90882,941848,90882,74408,74408,941848,81497836,94418,941848,90882,74408,90882,74408,90882,74408,90882,90882,90882,90882,90882,90882,74408,74408,94418,74408,9424,90882,90882,81497836,74408,941848,90882,91429743828,90882,90882,90882,90882,90882,90882,90882,90882,74408,90882,74408,90882,90882,74408,90882,91429743828,90882,90882,90882,90882,90882,91429743828,90882,74408,90882,941848,94418,91429743828,90882,74408,74408,74408,91429743828,74408,90882,9424,74408,90882,90882,90882,91429743828,90882,74408,90882,9448,90882,90882,90882,74408,90882,941848,90882,90882,74408,90882,941848,90882,74408,90882,74408,90882,74408,74408,90882,90882,74408,90882,74408,90882,90882,90882,90882,941848,74408,74408,90882,90882,941848,74408,91429743828,74408,81497836,74408,9448,74408,9424,74408,91429743828,90882,74408,74408,74408,90882,90882,90882,90882,954798,90882,9424,91429743828,90882,941848,90882,74408,90882,74408,954798,90882,74408,90882,90882,90882,9448,90882,90882,74408,90882,74408,74408,90882]
    file_name = dataset_name

    h5write(file_name ,"/ss_customer_sk",customer)
    h5write(file_name, "/ss_item_sk",sale)

    h5write(file_name, "/i_item_sk", item)
    h5write(file_name, "/i_class_id", class)
    h5write(file_name, "/i_category", category)

end

function create_dataset_small(dataset_name)
    #=
    customer    iterm
    1       1
    1       2
    2       1
    1       3
    2       3
    3       1
    =#
    customer = [1,1,2,1,2,3]
    sale = [1,2,1,3,3,1]

    #= item class category
    1   3   1
    2   1   2
    3   2   1
    =#
    item = [1,2,3]
    class = [3,1,2]
    category = [1,2,1]
    file_name = dataset_name

    h5write(file_name ,"/ss_customer_sk",customer)
    h5write(file_name, "/ss_item_sk",sale)

    h5write(file_name, "/i_item_sk", item)
    h5write(file_name, "/i_class_id", class)
    h5write(file_name, "/i_category", category)

end
                          
@acc hpat function q26(category, item_count, num_centroids, file_name)
    store_sales = DataSource(DataTable{:ss_item_sk=Int64,:ss_customer_sk=Int64}, HDF5, file_name)
    item = DataSource(DataTable{:i_item_sk=Int64,:i_category=Int64,:i_class_id=Int64}, HDF5, file_name)
    sale_items = join(store_sales, item, :ss_item_sk==:i_item_sk, :ss_item_sk)
    sale_items = sale_items[:i_category==category]

    customer_i_class = aggregate(sale_items, :ss_customer_sk, :ss_item_count = length(:ss_item_sk),
                                                               :id1 = sum(:i_class_id==1),
                                                               :id2 = sum(:i_class_id==2),
                                                               :id3 = sum(:i_class_id==3),
                                                               :id4 = sum(:i_class_id==4),
                                                               :id5 = sum(:i_class_id==5),
                                                               :id6 = sum(:i_class_id==6),
                                                               :id7 = sum(:i_class_id==7),
                                                               :id8 = sum(:i_class_id==8),
                                                               :id9 = sum(:i_class_id==9),
                                                               :id10 = sum(:i_class_id==10),
                                                               :id11 = sum(:i_class_id==11),
                                                               :id12 = sum(:i_class_id==12),
                                                               :id13 = sum(:i_class_id==13),
                                                               :id14 = sum(:i_class_id==14),
                                                               :id15 = sum(:i_class_id==15))

    customer_i_class = customer_i_class[:ss_item_count>item_count]
    return customer_i_class[:ss_item_count],customer_i_class[:id3]
end

function main()
    println("     Testing Query 26[small]")
    create_dataset_small("test_q26.hdf5")
    return_arr= q26(1, 1, 10, "test_q26.hdf5")
    @test return_arr[1] == [2,2]
    @test return_arr[2] == [1,1]
    rm("test_q26.hdf5")

    println("     Testing Query 26[large]")
    create_dataset_large("test_q26.hdf5")
    return_arr= q26(90882, 1, 10, "test_q26.hdf5")
    @test return_arr[1] == [3,2,2,2,2]
    @test return_arr[2] == [0,1,0,2,0]
    rm("test_q26.hdf5")

end

end

Q26_Test.main()
